package com.xiaoluo.a08_foreground;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityManager;
import android.app.Service;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.List;

import javax.security.auth.login.LoginException;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "xlll";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        Handler handler = new Handler();
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Log.i("xl packageName:", "run");
//                getForegroundApp();
//            }
//        }, 30000);
        Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  isAppForeground(MainActivity.this);
            }
        });
        button.performClick();
    }

//    private String getForegroundApp(Context context) {
//        UsageStatsManager usageStatsManager =
//                (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
//        long ts = System.currentTimeMillis();
//        List<UsageStats> queryUsageStats =
//                usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, 0, ts);
//        UsageEvents usageEvents = usageStatsManager.queryEvents( 0, ts);
//        if (usageEvents == null) {
//            return null;
//        }
//        UsageEvents.Event event = new UsageEvents.Event();
//        UsageEvents.Event lastEvent = null;
//        while (usageEvents.getNextEvent(event)) {
//            // if from notification bar, class name will be null
//            if (event.getPackageName() == null || event.getClassName() == null) {
//                continue;
//            }
//
//            if (lastEvent == null || lastEvent.getTimeStamp() < event.getTimeStamp()) {
//                lastEvent = event;
//            }
//        }
//
//        if (lastEvent == null) {
//            return null;
//        }
//        Log.i("xl packageName:",lastEvent.getPackageName());
//        return lastEvent.getPackageName();
//    }

    private void getForegroundApp() {
        UsageStatsManager m = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
        if (m != null) {
            long now = System.currentTimeMillis();
            //获取60秒之内的应用数据
            List<UsageStats> stats = m.queryUsageStats(UsageStatsManager.INTERVAL_BEST, now - 60 * 1000, now);
            Log.i(TAG, "Running app number in last 60 seconds : " + stats.size());
            String topActivity = "";
            //取得最近运行的一个app，即当前运行的app
            if ((stats != null) && (!stats.isEmpty())) {
                int j = 0;
                for (int i = 0; i < stats.size(); i++) {
                    if (stats.get(i).getLastTimeUsed() > stats.get(j).getLastTimeUsed()) {
                        j = i;
                    }
                    if (!stats.get(j).getPackageName().equals("com.swaiotos.appstore")) {
                        topActivity = stats.get(j).getPackageName();
                    }
                    Log.i(TAG, "top running app is : " + topActivity);
                }
                Log.i(TAG, "in the end running app is : " + topActivity);
            }
        }
    }



}
